package com.zjitc.chapter13.controller;

import com.zjitc.chapter13.exception.MyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;

@ControllerAdvice
public class ExceptionAdvice {
    @ExceptionHandler(MyException.class)
    public ModelAndView doMyException(MyException exception)throws IOException {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg", exception.getMessage());
        modelAndView.setViewName("error.jsp");
        return modelAndView;
    }
    @ExceptionHandler(Exception.class)
    public ModelAndView doOtherException(Exception exception) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg", "网络异常，请稍后再试！");
        modelAndView.setViewName("error.jsp");
        return modelAndView;
    }
}
